package com.xiangban.volunteer_platform.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangban.volunteer_platform.dao.BusinessUserMapper;
import com.xiangban.volunteer_platform.entity.BusinessUser;
import com.xiangban.volunteer_platform.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Mr. Ma
 * @Data 2021/1/20 11:58
 */
@Service
public class BusinessUserService {

    @Autowired
    private BusinessUserMapper businessUserMapper;
    @Autowired
    private PageUtil pageUtil;


    public int addUpdateUser(BusinessUser businessUser){
        if (businessUser.getId()==0){
           return businessUserMapper.insert(businessUser);
        }else {
           return businessUserMapper.updateById(businessUser);
        }

    }


    public int deleteUser(BusinessUser businessUser){
       return businessUserMapper.deleteById(businessUser.getId());
    }

    public BusinessUser findUserById(BusinessUser businessUser) {
      return  businessUserMapper.selectById(businessUser.getId());
    }

    public IPage<BusinessUser> findAllUser(Integer pageNum, Integer pageSize,BusinessUser businessUser) {
        IPage<BusinessUser> page = new Page<>(pageNum == null ? 1 : pageNum,
                pageSize == null ? 10 : pageSize);
        LambdaQueryWrapper<BusinessUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(BusinessUser::getName,businessUser.getName());

        return businessUserMapper.selectPage(page,queryWrapper);
//        IPage<BusinessUser> page= new Page<>(pageUtil.getPage().getPageNum(), pageUtil.getPage().getPageSize());
//        PageHelper.startPage(pageUtil.getPage().getPageNum(), pageUtil.getPage().getPageSize());
//        return  new PageInfo<>( businessUserMapper.selectList(new LambdaQueryWrapper<>()));
    }

    public List<BusinessUser> getABusinessllUser(){
        List<BusinessUser> businessUsers = businessUserMapper.selectList(new LambdaQueryWrapper<>());
        return businessUsers;
    }
}
